var utils = {
    timeFormat: function (date) {

        var _date;

        if (date.indexOf("今天") != -1) {

            _date = this.dateFormat(new Date());

            date =date.replace("今天",_date);
            date+=":00";
           
        } else if (date.indexOf("昨天") != -1) {

            _date = new Date();

            _date.setDate(_date.getDate() - 1);

            _date = this.dateFormat(_date);


            date = date.replace("昨天",_date);
            date+=":00";


        }else if (date.indexOf("前天") != -1) {

            _date = new Date();

            _date.setDate(_date.getDate() - 2);

            _date = this.dateFormat(_date);

            date = date.replace("前天",_date);
            date+=":00";

        } else if(date.indexOf("月") != -1){
            date = date.replace('月','-');
            date = date.replace('号','');

            _date = new Date().getFullYear();

            date = _date+"-"+date;
        }else{
           
        }
        
        _date = null;


        return new Date(date);


    },
    dateFormat(date) {

        var year = date.getFullYear(),
            month = (date.getMonth() + 1) > 10 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1),
            day = date.getDate() > 10 ? date.getDate() : "0" + date.getDate();
        return year + "-" + month + "-" + day;
    },
    trim: function (str) { //删除左右两端的空格
        var res;
        try {
            res = str.replace(/(^\s*)|(\s*$)/g, "");
        } catch (error) {
            
        }
        return res;
    },
    ltrim: function (str) { //删除左边的空格
        return str.replace(/(^s*)/g, "");
    },
    rtrim: function (str) { //删除右边的空格
        return str.replace(/(s*$)/g, "");
    },

    kkkmatchData:function(html){
        var DM5_VIEWSIGN = /\bDM5_VIEWSIGN\b="(.*?)";/ig;
        var DM5_VIEWSIGN_DT = /\bDM5_VIEWSIGN_DT\b="(.*?)";/ig;
        var DM5_MID = /\bDM5_MID\b=(.*?);/ig;
        var DM5_CID = /\bDM5_CID\b=(.*?);/ig;
        var DM5_IMAGE_COUNT = /\bDM5_IMAGE_COUNT\b=(.*?);/ig;
    
    
        var _sign = DM5_VIEWSIGN.exec(html);
        var _dt = DM5_VIEWSIGN_DT.exec(html);
        var _mid = DM5_MID.exec(html);
        var _cid = DM5_CID.exec(html);
        var totalPage = DM5_IMAGE_COUNT.exec(html);
    
    
        return {
            totalPage: Number(totalPage[1]),
            cid: Number(_cid[1]),
            page: 1,
            key: '',
            language: 1,
            gtk: 6,
            _cid: Number(_cid[1]),
            _mid: Number(_mid[1]),
            _dt: _dt[1],
            _sign: _sign[1]
        };
    },
    dmzjmatchData:function(html){

        console.log(html);

        var ev = /\beval\b\((.*)\)/ig;

        var match_eval = ev.exec(html);

        console.log(match_eval);

        return {
            totalPage:"("+match_eval[1]+")",
            page:1
        };
    },
    utf8to16:function(str) {
        var out, i, len, c;
        var char2, char3;
        out = "";
        len = str.length;
        i = 0;
        while (i < len) {
          c = str.charCodeAt(i++);
          switch (c >> 4) {
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
              out += str.charAt(i - 1);
              break;
            case 12: case 13:
              char2 = str.charCodeAt(i++);
              out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
              break;
            case 14:
              char2 = str.charCodeAt(i++);
              char3 = str.charCodeAt(i++);
              out += String.fromCharCode(((c & 0x0F) << 12) |
                ((char2 & 0x3F) << 6) |
                ((char3 & 0x3F) << 0));
              break;
          }
        }
  
        return out;
      }
};


module.exports = utils;